package me.luger.core.commom.util;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * HttpUtil
 *
 * @author luger
 * @date 2016/9/19 0019
 */
public class HttpUtil {

    /**
     * SCHEME字段
     */
    private static List<String> HEADER_SCHEME_KEY_LIST = new ArrayList<String>();

    /**
     * 请求头IP字段
     */
    private static List<String> HEADER_IP_KEY_LIST = new ArrayList<String>();

    /**
     * 请求头PORT字段
     */
    private static List<String> HEADER_PORT_KEY_LIST = new ArrayList<String>();

    /**
     * 特殊IP映射
     */
    private static Map<String, String> IP_MAP = new HashMap<String, String>();

    static {

        HEADER_SCHEME_KEY_LIST.add("X-Real-SCHEME");

        HEADER_IP_KEY_LIST.add("X-Real-IP");
        HEADER_IP_KEY_LIST.add("X-Forwarded-For");
        HEADER_IP_KEY_LIST.add("Proxy-Client-IP");
        HEADER_IP_KEY_LIST.add("WL-Proxy-Client-IP");
        HEADER_IP_KEY_LIST.add("HTTP_CLIENT_IP");
        HEADER_IP_KEY_LIST.add("HTTP_X_FORWARDED_FOR");

        HEADER_PORT_KEY_LIST.add("X-Real-PORT");

        IP_MAP.put("0:0:0:0:0:0:0:1", "127.0.0.1");
        IP_MAP.put("localhost", "127.0.0.1");
    }

    /**
     * @Title: String getRemoteAddr(HttpServletRequest request)
     * @Description: 从用户请求数据中获取访问IP
     * @param request
     * @return 获取的ip
     */
    public static String getRemoteAddr(HttpServletRequest request) {
        String ip = getHeadIpAddr(request);
        if (StringUtils.isBlank(ip)) {
            ip = request.getRemoteAddr();
        }
        String mapIp = (String) IP_MAP.get(ip);
        if (StringUtils.isNotBlank(mapIp)) {
            return mapIp;
        }
        return ip;
    }

    /**
     * @Title: String getHeadIpAddr(HttpServletRequest request)
     * @Description: 从请求头获取IP
     * @param request
     * @return 获取的ip
     */
    public static String getHeadIpAddr(HttpServletRequest request) {
        String ip = null;
        for (String ipKey : HEADER_IP_KEY_LIST) {
            ip = request.getHeader(ipKey);
            if (StringUtils.isNotBlank(ip)) {
                break;
            }
        }
        return ip;
    }

    public static boolean acceptsGzipEncoding(HttpServletRequest request) {
        return acceptsEncoding(request, "gzip");
    }

    public static boolean acceptsEncoding(HttpServletRequest request,
                                          String name) {
        final boolean accepts = headerContains(request, "Accept-Encoding", name);
        return accepts;
    }

    public static boolean headerContains(HttpServletRequest request,
                                         String header, String value) {
        Enumeration<?> accepted = request.getHeaders(header);
        while (accepted.hasMoreElements()) {
            String headerValue = (String) accepted.nextElement();
            if (headerValue.indexOf(value) != -1) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取请求的refere
     * @param request
     * @return
     */
    public static String getReferer(HttpServletRequest request){
        if(null != request){
            return request.getHeader("Referer");
        }
        return "";
    }
}
